import {pathToRegexp} from "path-to-regexp"
import lodash from 'lodash'
//根据图片路径动态加载图片
const dynamicLoadImg = (filePath, type = 'png') => {
  return new URL(`../assets/${filePath}.${type}`, import.meta.url).href
}
//获取子节点的所有父节点
const treeFindPath = (tree, func, field = "", path = []) => {
  if (!tree) return []
  for (const data of tree) {
    field === "" ? path.push(data) : path.push(data[field]);
    if (func(data)) return path
    if (data.children) {
      const findChildren = treeFindPath(data.children, func, field, path)
      if (findChildren.length) return findChildren
    }
    path.pop()
  }
  return []
}
//根据path匹配菜单路径
const matchRouterMenus = (path, routerMenus, matchMenus = []) => {
  for (const menu of routerMenus) {
    matchMenus.push(menu)
    const result = menu.path && pathToRegexp(menu.path)
    const isMatch = result && result.exec(path)
    if (isMatch) {
      return matchMenus
    }
    if (menu.children) {
      const matchChildMenus = matchRouterMenus(path, menu.children, matchMenus)
      if (matchChildMenus.length) {
        return matchChildMenus
      }
    }
    matchMenus.pop()
  }
  return []
}
//根据路由生成layout menu菜单
const getMenus = (routers) => {
  const copyRouters = lodash.cloneDeep(routers)
  //过滤出包含label的节点,如果子节点包含那么父节点的层级结构也在
  const filter = (routers) => {
    return routers.filter(router => {
      const {meta = {}, children} = router
      const {label} = meta
      if (children && children.length > 0) {
        router.children = filter(children)
      }
      return label || (router.children && router.children.length > 0)
    })
  }
  //扁平纯粹包含label的节点
  const array = []
  const flatten = (routers) => {
    routers.map(router => {
      const {meta = {}, children} = router
      const {label} = meta
      if (label) {
        array.push(router)
      } else {
        flatten(children)
      }
    })
  }
  //转化成menu格式
  const transform = (routers) => {
    return routers.map(router => {
      const {path, meta = {}, children} = router
      const {flatten, ...rest} = meta
      if (children && children.length > 0) {
        router.children = transform(children)
      }
      return {path, key: path, ...rest, children: router.children}
    })
  }
  const filterMenu = filter(copyRouters)
  flatten(filterMenu)
  return transform(array)
}
//根据key-value从tree中找出相应object
const findSelectObj = (key, value, tree) => {
  let select = undefined
  const find = (array) => {
    array.map((menu) => {
      if (menu[key] === value) {
        select = menu
        return
      }
      if (menu.children && menu.children.length > 0) {
        find(menu.children)
      }
    })
  }
  find(tree)
  return select
}
//线性渐变色从上到下
const linearColor = (color1, color2) => ({
  type: 'linear',
  x: 0,
  y: 0,
  x2: 0,
  y2: 1,
  colorStops: [
    {color: color1, offset: 0},
    {color: color2, offset: 1}
  ],
})
const hexToRgba = (hex,alpha=1)=>{
  // 确保十六进制代码以“#”开始
  hex = hex.replace(/^#?/, "#");
  // 解析十六进制颜色
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  return `rgb(${r},${g},${b},${alpha})`
}
export {
  dynamicLoadImg,
  treeFindPath,
  findSelectObj,
  linearColor,
  matchRouterMenus,
  getMenus,
  hexToRgba
}
